<?php

/*
 * Copyright (C) 2014 Peslook
 *                    Vietnam
 *                    http://www.peslook.com
 *                    support@peslook.com
 *
 * Authors:
 *  Loi Nguyen <loint@peslook.com>
 *
 * This file is part of the peslook project
 */

namespace App\Controller;

use App\Library\System\Config;
use App\Library\System\Model\AjaxModel;
use App\Library\System\Model\SearchModel;
use App\Library\System\Model\UserModel;
use App\Library\System\Process;
use App\Library\System\Model\WidgetModel;
use Zend\View\Helper\ViewModel;

/**
 * Ajax Controller
 *
 * @category   App
 * @package    App\Controller
 * @author     Loi Nguyen <loint@peslook.com>
 * @copyright  Peslook
 * @version    1.0
 * @since      Class available since Release 1.0
 */
class AjaxController extends BaseController
{

    /**
     * Ajax Model
     *
     * @var AjaxModel
     */
    public $ajax;

    /**
     * Process function
     *
     * @var Process
     */
    public $process;

    /**
     * Configuaration class
     *
     * @var Config
     */
    public $config;

    /**
     * Constructor
     * Initialize ajax, process and config
     *
     */
    public function __construct()
    {
        $this->ajax = AjaxModel::getInstance();
        $this->process = Process::getInstance();
        $this->config = Config::getInstance();
    }

    /**
     * Ajax index page
     *
     * @router /syn
     * @return Error Page not found
     */
    public function indexAction()
    {
        return $this->error(404);
    }

    /**
     * Get Widget
     *
     * @return void|\Zend\View\Model\ViewModel
     */
    public function getWidgetAction()
    {
        $id = $this->post('widget_id');
        $widget = new WidgetModel($id);

        // Widget is not enable
        if (!$widget->isEnable()) {
            return $this->error(404);
        }

        // Set up forder layout
        $this->setAjaxLayout($widget, 'widget');

        // Choose template suitable with widget
        $widget->setTemplate($widget->alias);

        // Create widget
        $page = $widget->create();

        $page->abc = $this->translate('Your University ?');

        return $page;
    }

    public function searchAction()
    {
        $query = $this->get('query');
        //key is not available
        if ($query == '') {
            return $this->error(404);
        }

        $search = new SearchModel($query);

        $result = array();
        $result['people'] = $search->getSuggestUser()->toArray();
        $result['skill'] = $search->getSuggestSkill()->toArray();

        return $this->json($result, true);
    }

    public function updateInfoAction()
    {


    }

    public function checkUserAction()
    {

    }

    public function changeAvatarAction()
    {
        $avatar = $this->post();
        $this->secure($avatar);
        $this->ajax->updateAvatar($avatar);

        $user = new UserModel($avatar->uid);
        $original = Config::path($this->config->root . '/img/' . Config::img_app_avatar_original . '/' . $user->alias . '.jpg');
        $current = Config::path($this->config->root . '/img/' . Config::img_app_avatar_current . '/' . $user->alias . '.jpg');

        $this->process->cropImage($original, $current, $avatar->x, $avatar->y, $avatar->s, $avatar->s);

        die();

        /* $original =
          $current = $user->alias

          //$this->process->cropImage($avatar);
         */
        $response = array(
            'success' => 1
        );

        echo 'OK';
        die();

        //return $this->json($response);
    }

    /**
     * Check Existing Email
     *
     * @return null|string|\Zend\View\Model\JsonModel
     */
    public function checkEmailAction() {
        $data = $this->post();
        $email = $data->email;
        if ($this->ajax->isExistedEmail($email)) {
            $message = 'true';
        } else {
            $message = 'false';
        }

        return $this->json(array(
            'message' => $message
        ));
    }


}
